import queryString from 'querystring'
let intervalId

class PostMessageUtils {
  // post用于显示的信息
  static postShowMessage(domain, targetWindow, params) {
    const queryParams = queryString.parse(location.search.substring(1, location.search.length))
    console.log(queryParams)
    const graphConfig = Object.assign({
      isHeaderVisible: false, // 头部是否显示
      isSidebarVisible: false, // 左侧菜单是否显示
      topHref: encodeURIComponent(window.location.href),
      keyword: queryParams.keyword,
      id: queryParams.id,
      ...queryParams
    }, params)

    // graphConfig.tgt = SsoSdk.utils.getTicket(config.TICKET_KEY)

    // const currTenant = CacheUtils.getOrSetCurrTenant()
    // graphConfig.tenantCode = currTenant.tenantId

    graphConfig.tgt = "TGT-1087-2CFINkHKDbBGMKeJCFMT54O5w6IzUTiuJj0zHFsdpzAo1idi4I-x-sso"
    graphConfig.tenantCode = "d840ef7567774710bad993ae4eef0f60"
    targetWindow.postMessage(JSON.stringify(graphConfig), domain)
    console.log('已post数据' + domain, JSON.stringify(graphConfig))
  }

  // 轮询发送 默认30秒
  static autoPostMessageByPoll(domain, targetWindow, params, pollCount = 30) {
    clearInterval(intervalId)
    let i = pollCount || 60
    intervalId = setInterval(() => {
      PostMessageUtils.postShowMessage('*', targetWindow, params)
      i--
      if (i <= 0) clearInterval(intervalId)
    }, 1e3)
  }
}

export default PostMessageUtils
